package com.service.impl;

import com.constant.Constant;
import com.dao.ResourceMapper;
import com.entity.Resource;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.service.ResourceService;
import com.util.AjaxResult;
import com.vo.ResourceVo;
import com.vo.UserResource;
import org.springframework.beans.factory.annotation.Autowired;

import com.dao.ResourceMapper;
import com.github.pagehelper.PageInfo;
import com.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.text.resources.FormatData;

import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    ResourceMapper resourceMapper;
    private PageInfo<Resource> info;

    //    潘军 同名方法
//    @Override
//    public PageInfo findBackResource(int id) {
//        List list = resourceMapper.findBackResource(id);
//        PageInfo info = new PageInfo(list);
//        return info;
//    }

//    用户删除资源 pj
    @Override
    public AjaxResult removeFrontResource(int id) {
        AjaxResult result=new AjaxResult(true,"",null);
        int i = resourceMapper.removeFrontResource(id);
        if (i==1){
            result.setMsg("修改成功");
        }else{
            throw  new ServiceException("修改失败");
        }
        return  result;
    }

    //    潘军 添加资源
    public int addFrontResource(FormatData formatData, User user) {
        return 1;
    }

    //    后台查询资源 pj
    @Override
    public PageInfo<Resource> findBackResource(int pageno, ResourceVo resourceVo) {
//        AjaxResult result=new AjaxResult(true,"",null);
        PageInfo<Resource> info=new PageInfo<>();
        PageHelper.startPage(pageno, Constant.PAGE_SIZE);
        List<Resource> backResource = resourceMapper.findBackResource(resourceVo);
        if(backResource!=null){
            info.setList(backResource);
        }else {
            throw new ServiceException("查询失败");
        }
              return info;

    }
//pj 修改启用禁用
    @Override
    public AjaxResult toggleStatus(Resource resource) {
        AjaxResult result=new AjaxResult(true,"",null);
        int i = resourceMapper.toggleStatus(resource);
        if (i==1){
            result.setMsg("修改成功");
        }else{
            throw  new ServiceException("修改失败");
        }
        return  result;
    }

    //根据id查询resource pj
    @Override
    public Resource findResourceByid(Integer resourceId) {
        Resource resourceByid = resourceMapper.findResourceByid(resourceId);
        return resourceByid;
    }
        //李奇洲  查询用户资源购买记录
    @Override
    public UserResource selectUserResourceByUidAndResId(Integer uid, Integer resid) {
        return resourceMapper.selectUserResourceByUidAndResId(uid,resid);
    }

    @Override
    public int insertUserResource(UserResource resource) {
        return resourceMapper.insertUserResource(resource);
    }

    //   @Lqz 资源校验
    //   验证用户积分或金币是否足够


    //@李奇洲
    @Override
    public Resource selectResourceByResourceId(Integer resource_id) {
        return resourceMapper.selectResourceByResourceId(resource_id);

    }


}

